/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.KaSupplier;
import com.kaaction.modules.ka.service.KaSupplierService;

/**
 * 供应商Controller
 * @author huanghaiming
 * @version 2017-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/kaSupplier")
public class KaSupplierController extends BaseController {

	@Autowired
	private KaSupplierService kaSupplierService;
	
	@ModelAttribute
	public KaSupplier get(@RequestParam(required=false) String id) {
		KaSupplier entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kaSupplierService.get(id);
		}
		if (entity == null){
			entity = new KaSupplier();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:kaSupplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(KaSupplier kaSupplier, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KaSupplier> page = kaSupplierService.findPage(new Page<KaSupplier>(request, response), kaSupplier); 
		model.addAttribute("page", page);
		return "modules/ka/kaSupplierList";
	}

	@RequiresPermissions("ka:kaSupplier:view")
	@RequestMapping(value = "form")
	public String form(KaSupplier kaSupplier, Model model) {
		model.addAttribute("kaSupplier", kaSupplier);
		return "modules/ka/kaSupplierForm";
	}

	@RequiresPermissions("ka:kaSupplier:edit")
	@RequestMapping(value = "save")
	public String save(KaSupplier kaSupplier, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kaSupplier)){
			return form(kaSupplier, model);
		}
		int cnt = kaSupplierService.getEqual(kaSupplier);
		if(cnt>0){
			model.addAttribute("message", "该供应商已存在");
			return form(kaSupplier, model);
		}
		kaSupplierService.save(kaSupplier);
		addMessage(redirectAttributes, "保存供应商成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaSupplier/?repage";
	}
	
	@RequiresPermissions("ka:kaSupplier:delete")
	@RequestMapping(value = "delete")
	public String delete(KaSupplier kaSupplier, RedirectAttributes redirectAttributes) {
		kaSupplierService.delete(kaSupplier);
		addMessage(redirectAttributes, "删除供应商成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaSupplier/?repage";
	}

}